﻿using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;
using RabbitMQ.Client;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TMS_Master.Entities;

namespace TMS_Master.RabbitMQ
{
    public class RabbitMQController:Controller
    {
        //调用生产者
        public IActionResult Data()
        {
            //模拟数据
            List<Cargo_Information> Cargo_Info = new List<Cargo_Information>()
            {
                new Cargo_Information{CargoName="轮胎",SkuName = "1200R20",Number =10 ,UseInfo = "车库备胎数量不够",Proposer = "李晓红",Remark = "--",CreateTime =DateTime.Now },
                new Cargo_Information{CargoName="轮胎",SkuName = "1200R20",Number =10 ,UseInfo = "车库备胎数量不够",Proposer = "李晓红",Remark = "--",CreateTime =DateTime.Now },
                new Cargo_Information{CargoName="轮胎",SkuName = "1200R20",Number =10 ,UseInfo = "车库备胎数量不够",Proposer = "李晓红",Remark = "--",CreateTime =DateTime.Now },
                new Cargo_Information{CargoName="轮胎",SkuName = "1200R20",Number =10 ,UseInfo = "车库备胎数量不够",Proposer = "李晓红",Remark = "--",CreateTime =DateTime.Now },
                new Cargo_Information{CargoName="轮胎",SkuName = "1200R20",Number =10 ,UseInfo = "车库备胎数量不够",Proposer = "李晓红",Remark = "--",CreateTime =DateTime.Now },
                new Cargo_Information{CargoName="轮胎",SkuName = "1200R20",Number =10 ,UseInfo = "车库备胎数量不够",Proposer = "李晓红",Remark = "--",CreateTime =DateTime.Now },
            };
            //调用方法
            //Index(students);
            return View();
        }


        //调用消费者
        public IActionResult GetData()
        {
            //调用方法
            Receiver();
            return View();
        }


        //发送者
        public IActionResult Index()
        {
            //此处我们链接本地主机所以 HostName = "localhost",
            //如果我们打算链接其他机器上的代理，这里得写上那台机器的机器名或者IP地址。
            var factory = new ConnectionFactory() { HostName = "localhost", UserName = "guest", Password = "guest", Port = 5672 };

            using (var connection = factory.CreateConnection())
            {
                using (var channel = connection.CreateModel())
                {
                    //声明队列
                    channel.QueueDeclare(queue: "采购", durable: false, exclusive: false, autoDelete: false, arguments: null);

                    //消息
                    string message = JsonConvert.SerializeObject("测试Mq" + new Random().Next(1000, 9999).ToString());
                    //编码
                    var body = Encoding.UTF8.GetBytes(message);
                    //发布一条消息路由键必须小于255字节。
                    channel.BasicPublish(exchange: "", routingKey: "采购", basicProperties: null, body: body);

                }
            }

            return View();
        }

        //接收者
        public void Receiver()
        {
            //定义变量默认为true
            bool i = true;
            //数据
            List<Cargo_Information> cargos = new List<Cargo_Information>();

            //创建连接
            var factory = new ConnectionFactory() { HostName = "localhost", UserName = "guest", Password = "guest", Port = 5672 };
            using (var connection = factory.CreateConnection())
            {
                using (var channel = connection.CreateModel())
                {
                    //声明一个队列（这个队列必须和发布者队列一致）
                    channel.QueueDeclare(queue: "采购", durable: false, exclusive: false, autoDelete: false, arguments: null);

                    //循环
                    while (i)
                    {
                        //获取RabbitMq一条数据
                        BasicGetResult result = channel.BasicGet("采购", true);
                        //判断是否为空
                        if (result != null)
                        {
                            //将数据添加到students
                            string data = Encoding.UTF8.GetString(result.Body.ToArray());
                            Cargo_Information cargo = JsonConvert.DeserializeObject<Cargo_Information>(data);
                            cargos.Add(cargo);
                        }
                        else
                        {
                            i = false;
                        }
                    }
                }
            }
        }
    }
}

